package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.service.DubboCartService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Reference(check = false)
    private DubboCartService dubboCartService;

    @RequestMapping("/show")
    public String cartShow(Model model){
        long userId=7L;
        List<Cart> cartList=dubboCartService.findCartListByUserId(userId);
        model.addAttribute("cartList", cartList);
        return "cart";
    }
    /*
    * http://www.jt.com/cart/update/num/114514*/
    @RequestMapping("/update/num/{itemId}/{num}")
    @ResponseBody
    public void updateNum(Cart cart){
        long userId=7L;
        cart.setUserId(userId);
        dubboCartService.updateNum(cart);
    }
    /*
    * http://www.jt.com/cart/add/114514*/
    @RequestMapping("/add/{itemId}")
    public String addCart(Cart cart){
        Long userId=7L;
        cart.setUserId(userId);
        dubboCartService.addCart(cart);
        return "redirect:/cart/show.html";
    }
    @RequestMapping("/delete/{itemId}")
    public String deleteCarts(Cart cart){
        long userId=7;
        cart.setUserId(userId);
        dubboCartService.deleteCart(cart);
        return "redirect:/cart/show.html";
    }

}
